<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<script>

  // function timeout(ms) {
  //   return new Promise((resolve,reject)=>{
  //     resolve(ms)
  //   })
  // }
  // timeout(100).then((value)=>{
  //   console.log(value);
  // });
  //

  //异步加载图片
  function loadImageAsync(url) {
    return new Promise(function (resolve, reject) {
      const image = new Image();
      image.onload = function () {
        resolve(image);
      };
      image.onerror = function () {
        reject(new Error('error'));
      };
      image.src = url;
    });
  }
  const getJSON = function (url) {
    const promise = new Promise(function (resolve, reject) {
      const client = new XMLHttpRequest();
      client.open("GET", url);
      client.onreadystatechange = function () {
        if (this.readyState !== 4) {
          return;
        }
        if (this.status === 200) {
          resolve(this.response);
        } else {
          reject(new Error(this.statusText));
        }
      };
      client.responseType = "json";
      client.setRequestHeader("Accept", "application/json");
      client.send();
    });

    return promise;
  };

  getJSON("./test.json").then(function (json) {
    console.log(json);
  }, function (error) {
    console.error('出错了', error);
  });


  // let promise = new Promise(function(resolve,reject){
  //   console.log('Promise');
  //   resolve('next');
  // }).then(function (next) {
  //   console.log(next);
  //
  // }).catch(function (err) {
  //   throw new Error(err)
  // })


</script>
</body>
</html>